#!/bin/sh

# Startup script for amavisd-new daemon for use on Slackware Linux x86|x86_64

# Copyright (c) 2008-2019, Nishant Limbachia, Hoffman Estates, IL, USA
# (nishant _AT_ mnspace _DOT_ net)
# Usage: /etc/rc.d/rc.amavisd-new start|stop|restart|reload|status
# For automatic startup at boot, call this script from rc.local

# Notes - 03/25/2013 #
# With v2.8.0, amavis-mc daemon was added to the mix and so this rc script
# has been overhauled from the previous version.  If you have suggestions to
# improve, please feel free to share.

# Script starts three different daemons: amavis-mc, amavisd-signer & amavisd
# All the daemons have their own start and stop functions, however, amavisd
# also has restart and reload which are supported by the program.

# Assuming you want all 3 daemons, you can start all at once like this:
# /etc/rc.d/rc.amavisd-new start


MC_PID=/var/run/amavis/amavis-mc.pid
PID=/var/run/amavis/amavisd.pid
AMAVIS_USER=@AMAVIS_USER@
AMAVIS_GROUP=@AMAVIS_GROUP@

amavisd_signer_start() {
	# start amavisd-signer
	printf "Starting amavisd-signer daemon\n"
	/usr/sbin/amavisd-signer
}

amavisd_signer_stop() {
	# stop amavisd-signer first
	printf "Stopping amavisd-signer daemon\n"
	pkill amavisd-signer
}

amavis_mc_start() {
	# start amavis-mc process
	if [ -f $MC_PID ]; then
	    printf "amavis-mc daemon running with PID: $(cat $MC_PID)\n"
	    printf "Terminating previous amavis-mc process\n"
	    kill $(cat $MC_PID)
	    rm -f $MC_PID
	    printf "Starting amavis-mc daemon\n"
	    /usr/sbin/amavis-mc -P $MC_PID
	else
	    printf "Starting amavis-mc daemon\n"
	    mkdir -p $(dirname $MC_PID)
	    chown $AMAVIS_USER:$AMAVIS_GROUP $(dirname $MC_PID)
	    chmod 0770 $(dirname $MC_PID)
	    /usr/sbin/amavis-mc -P $MC_PID
	fi
}

amavis_mc_stop() {
	if [ -f $MC_PID ]; then
	    printf "Stopping amavis-mc daemon\n"
	    kill $(cat $MC_PID)
	    rm -f $MC_PID
	else
	    printf "amavis-mc daemon is not running\n"
	fi
}

amavisd_start() {
  	if [ -f $PID ]; then
	    printf "amavisd-new daemon running with PID: $(cat $PID)\n"
	    printf "Terminating previous amavisd-new process\n"
	    kill $(cat $PID)
	    rm -f $PID
	    printf "Starting amavisd-new daemon\n"
	    /usr/sbin/amavisd start
	else
	    printf "Starting amavisd-new daemon\n"
	    mkdir -p $(dirname $PID)
	    chown $AMAVIS_USER:$AMAVIS_GROUP $(dirname $PID)
	    chmod 0770 $(dirname $PID)
	    /usr/sbin/amavisd start
	fi
}

amavisd_stop() {
	if [ -f $PID ]; then
	    printf "Stopping amavisd-new daemon\n"
	    /usr/sbin/amavisd stop
	    rm -f $PID
	else
	    printf "amavisd-new daemon is not running\n"
	fi	
}

amavisd_reload() {
	echo "Reloading amavisd-new daemon"
	/usr/sbin/amavisd reload
}

amavisd_restart() {
	printf "Restarting amavisd-new daemon\n"
	/usr/sbin/amavisd restart
}

### This is where all the combined processes start

daemons_start() {
    amavis_mc_start
    sleep 2
    amavisd_signer_start
    sleep 2
    amavisd_start
}

daemons_stop() {
    amavisd_stop
    amavisd_signer_stop
    amavis_mc_stop
}

daemons_restart() {
    amavis_mc_stop
    sleep 2
    amavis_mc_start
    
    amavisd_signer_stop
    sleep 2
    amavisd_signer_start
    
    sleep 2
    amavisd_restart
}

daemons_status() {
    printf "amavis-mc daemon running with PID: $(cat $MC_PID)\n"
    printf "amavisd-new daemon running with PID: $(cat $PID)\n"
    printf "amavisd-signer daemon running with PID: $(pgrep amavisd-signer)\n"
}

### 

case "$1" in
'start')
	daemons_start
  ;;
'stop')
	daemons_stop
  ;;
'restart')
	daemons_restart
  ;;
'reload')
	amavisd_reload
  ;;
'status')
	daemons_status
  ;;
*)
	echo "USAGE: $0 start|stop|restart|reload|status"
	exit 1
  ;;
esac
